<?php

namespace app\channel\model;

use app\BaseModel;
use app\shop\model\GoodsModel;
use think\Db;
use think\Exception;
use think\facade\Cache;

use app\member\model\UserAddressModel;

//*------------------------------------------------------ */
//-- 代理订单
/*------------------------------------------------------ */

class OrderModel extends BaseModel
{
    protected $table = 'channel_order_info';
    public $pk = 'order_id';
    protected $mkey = 'channel_order_mky_';
    /*------------------------------------------------------ */
    //-- 清除缓存
    /*------------------------------------------------------ */
    public function cleanMemcache($order_id = 0)
    {
        Cache::rm($this->mkey . $order_id);
        Cache::rm($this->mkey . '_goods_' . $order_id);
    }
    /*------------------------------------------------------ */
    //-- 生成订单编号
    /*------------------------------------------------------ */
    public function getOrderSn()
    {
        /* 选择一个随机的方案 */
        mt_srand((double)microtime() * 1000000);
        $date = date('Ymd');
        $order_sn = 'ws' . $date . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $where[] = ['order_sn', '=', $order_sn];
        $where[] = ['add_time', '>', strtotime($date)];
        $count = $this->where($where)->count('order_id');
        if ($count > 0) return $this->getOrderSn();
        return $order_sn;
    }
    /*------------------------------------------------------ */
    //-- 写入订单日志
    /*------------------------------------------------------ */
    function _log(&$order, $logInfo = '')
    {
        return OrderLogModel::_log($order, $logInfo);
    }
    /*------------------------------------------------------ */
    //-- 写入订单
    /*------------------------------------------------------ */
    public function add($post)
    {
        $purchaseType = intval($post['purchaseType']);
        $rec_id = trim($post['rec_id']);
        $CartModel = new CartModel();
        $ProxyUsersModel = new ProxyUsersModel();
        $ChannelGoodsModel = new ChannelGoodsModel();
        $GoodsModel = new GoodsModel();
        $cartList = $CartModel->getCartList($purchaseType, 1, $rec_id);
//        var_dump($cartList);die;
        if (empty($cartList) == true) {
            return '请选择订购商品.';
        }
        $purchaseTypeList = config('config.purchaseTypeList');
        if (empty($purchaseTypeList[$purchaseType])) {
            return '不存在相关进货类型.';
        }
        $inArr['buyer_message'] = trim($post['buyer_message']);
        $inArr['order_status'] = 0;
        $inArr['pay_status'] = 0;
        $inArr['shipping_status'] = 0;
        $inArr['is_pay'] = $purchaseType == 3 ? 0 : 1;
        $inArr['shipping_fee'] = 0;
        if ($purchaseType > 1) {
            $address_id = intval($post['address_id']);
            if ($address_id < 0) return '请设置收货地址后，再操作.';
            $addressList = (new UserAddressModel)->getRows();
            $address = $addressList[$address_id];
            if (empty($address)) return '相关收货地址不存在.';
            $inArr['consignee'] = $address['consignee'];
            $inArr['address'] = $address['address'];
            $inArr['merger_name'] = $address['merger_name'];
            $inArr['province'] = $address['province'];
            $inArr['city'] = $address['city'];
            $inArr['district'] = $address['district'];
            $inArr['mobile'] = $address['mobile'];
            //运费处理
            $shippingFee = $CartModel->_evalShippingFee($address, $cartList);
            $inArr['shipping_fee'] = $shippingFee['shipping_fee'];
        }
        $time = time();
        $goods_amount = 0;
        $goods_number = 0;
        $buy_goods_sn = [];
        $buy_goods_id = [];
        foreach ($cartList as $cg) {
            $buy_goods_sn[] = $cg['goods_sn'];
            $buy_goods_id[] = $cg['goods_id'];
            $goods_amount += $cg['sale_price'] * $cg['real_number'];
            $goods_number += $cg['real_number'];
        }
        $ProxyLevelModel = new ProxyLevelModel();
        if ($purchaseType == 1) { // 补货订单
            if ($post['proxy_id'] > 0) {
                // 当前身份
                $proxy = $ProxyLevelModel->info($post['proxy_id']);
                $new_order = $this->where('user_id', $this->userInfo['user_id'])
                    ->where('order_status', 1)->where('pay_status', 1)->sum('order_amount');
                if ($new_order + $goods_amount < $proxy['replenish_money']) {
                    return '本次升级需要补货' . ($proxy['replenish_money'] - $new_order) . '元';
                }
                //是否为升级订单:0-否;1-是
                $inArr['order_upgrade_status'] = 1;
            } else {
                $user = (new ProxyUsersModel())->info($this->userInfo['user_id']);
                if ($user['proxy_id'] < 1) {
                    $min_level = $ProxyLevelModel->order('level', 'desc')->find();
                    if ($goods_amount < $min_level['replenish_money']) {
                        return '首次补货升级成代理需补货' . $min_level['replenish_money'] . '元';
                    }
                    //是否为升级订单:0-否;1-是
                    $inArr['order_upgrade_status'] = 1;
                } else if ($goods_number < $user['proxyLevel']['replenish_limit']) {
                    return '每次补货最低' . $user['proxyLevel']['replenish_limit'] . '件';
                }
            }
        } else { // 提货
            $user = (new ProxyUsersModel())->info($this->userInfo['user_id']);

            if ($user['proxy_id'] > 0) {
                //提示时的代理上级
                $inArr['agent_pid'] = $user['supply_uid'];
                //代理商提货审核状态:0-不需要审核;1-待审核;2-审核通过;3-审核失败
                $inArr['delivery_audit_status'] = 1;
//                var_dump($goods_amount);
//                var_dump($user);
//
//                die;
                if ($goods_number < $user['proxyLevel']['extract_limit']) {
                    return "每次提货最低{$user['proxyLevel']['extract_limit']}件";
                }
            } else {
                return '当前身份不是代理，不能提货';
            }
        }

        $inArr['goods_amount'] = $goods_amount;
        $inArr['buy_goods_sn'] = join(',', $buy_goods_sn);
        $inArr['buy_goods_id'] = join(',', $buy_goods_id);
        $inArr['balance_deduction'] = 0;
        $inArr['is_deduct'] = 0;
        $inArr['balance_deduction'] = 0;
        // 供货差价
        $supply_amount = 0;
        $StockModel = new StockModel();
        if ($purchaseType == 3) {//提货订单
            $lackGoods = $StockModel->checkLackGoods($cartList, $this->userInfo['user_id'], $purchaseType);//验证库存
            if (empty($lackGoods) == false) {
                return '库存不足，不能下单.';
            }
            $inArr['is_stock'] = 1;
            $inArr['order_amount'] = $inArr['shipping_fee'];
            if ($inArr['order_amount'] <= 0) {
                $inArr['is_pay'] = 0;//不需要支付
                $inArr['order_status'] = 1;
                $inArr['confirm_time'] = $time;
                $inArr['is_pay_eval'] = 1;
            } else {
                $inArr['is_pay'] = 1;
            }
            $inArr['supply_uid'] = $this->userInfo['user_id'];//提货订单供货人为自己
        } else {
            $inArr['is_pay'] = 1;//需要支付
            $inArr['order_amount'] = $goods_amount + $inArr['shipping_fee'];//暂不考虑运费，订单直接等于商品金额
            $inArr['supply_uid'] = $this->userInfo['supply_uid'];
            // 最后充值记录
            $RechargeModel = new RechargeLogModel();
            $recharge_info = $RechargeModel->where('user_id', $this->userInfo['user_id'])
                ->where('status', 9)->where('type', 'earnestMoney')
                ->order('pay_time', 'desc')->find();
            if (empty($recharge_info) == false && $inArr['supply_uid'] == $recharge_info['supply_uid']
                && $recharge_info['check_time'] >= (time() - 365 * 86400)) {
                // 供货差价
                if ($inArr['supply_uid'] < 1) {
                    $proxy_id = 0;
                } else {
                    $supply_uid = $ProxyUsersModel->info($inArr['supply_uid']);
                    $proxy_id = $supply_uid['proxy_id'];
                }
                foreach ($cartList as $kcg => $cg) {
                    $channelGoods = $ChannelGoodsModel->info($cg['goods_id']);//代理商品数据
                    $channel_goods = $ChannelGoodsModel->getShopGoods($cg['goods_id']);
                    $channel_goods['price_type'] = $channelGoods['price_type'];
                    $channel_goods['pricing_type'] = $channelGoods['pricing_type'];
                    $channel_goods['is_on_sale'] = $channelGoods['is_on_sale'];
                    $channel_goods['unit'] = $channelGoods['unit'];
                    $channel_goods['convert_unit'] = $channelGoods['convert_unit'];
                    $channel_goods['unitLits'] = (new GoodsUnitModel)->getRows();
                    $buy_goods_sn[] = $cg['goods_sn'];
                    $buy_goods_id[] = $cg['goods_id'];
                    $goods_amount += $cg['sale_price'] * $cg['real_number'];
                    $goods_number += $cg['real_number'];
                    if ($proxy_id < 1) {
                        #vender-厂家价格   real_number-订购数量    退还保证金
                        //$return_balance_deduction = $channelGoods['vender'] * $cg['real_number'];
                        $return_balance_deduction = $channelGoods['refund_deposit'] * $cg['real_number'];
                        $supply_amount += $return_balance_deduction;
                        $cartList[$kcg]['balance_deduction'] = $return_balance_deduction;
                    } else {
                        //$supply_price = $ChannelGoodsModel->getPrcies($channel_goods, $proxy_id, true);
                        //退还保证金
                        //$supply_price_all = $supply_price['price'] * $cg['real_number'];
                        $supply_price_all = $channelGoods['refund_deposit'] * $cg['real_number'];
                        $supply_amount += $supply_price_all;
                        $cartList[$kcg]['balance_deduction'] = $supply_price_all;
                    }
                }
                $WalletModel = new WalletModel();
                $account = $WalletModel->where('user_id', $this->userInfo['user_id'])->find();
                //待还保证金总额
                $totalBond = Db::name('channel_proxy_bond')->where('user_id', $this->userInfo['user_id'])->value('amount_be_returned');
                //剩余保证金总额
                $earnest_money = $account['earnest_money'] - $totalBond;
                if ($supply_amount > $earnest_money) { // 超过保证金
                    $inArr['balance_deduction'] = $earnest_money;
                } else {
                    $inArr['balance_deduction'] = $supply_amount;
                }
                //取消订单扣除保证金
//                $inArr['order_amount'] -= $inArr['balance_deduction'];
                //判断是否是退保订单
                if ($inArr['balance_deduction'] > 0) {
                    $inArr['surrender_order_status'] = 1;
                }
            }
        }

        $ProxyUsersModel = new ProxyUsersModel();
        if ($inArr['supply_uid'] > 0) {//获取供货上级当时层级
            $supplyUserInfo = $ProxyUsersModel->info($inArr['supply_uid']);
            $inArr['supply_proxy_id'] = $supplyUserInfo['proxy_id'];
        }

        $inArr['buyer_message'] = input('desc', '', 'trim');
        $inArr['purchase_type'] = $purchaseType;
        $inArr['add_time'] = $time;
        $inArr['user_id'] = $this->userInfo['user_id'];
        $inArr['user_proxy_id'] = $this->userInfo['proxy_id'];

        $inArr['user_pid'] = $this->userInfo['pid'];
        if ($inArr['user_pid'] > 0) {//获取推荐上级当时层级
            $pUserInfo = $ProxyUsersModel->info($inArr['user_pid']);
            $inArr['user_pid_proxy_id'] = $pUserInfo['proxy_id'];
        }

        $inArr['add_time'] = $time;
        $inArr['order_sn'] = $this->getOrderSn();

        if ($inArr['balance_deduction'] > 0) {
            $inArr['order_amount'] = $inArr['order_amount'] - $inArr['balance_deduction'];
        }

        Db::startTrans();//启动事务
        $res = $this->save($inArr);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '未知原因，订单写入失败.';
        }
        $order_id = $this->order_id;
        $_log = '生成' . $purchaseTypeList[$purchaseType];
        $inArr['order_id'] = $order_id;
        $res = $this->_log($inArr, $_log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '未知原因，订单日志写入失败.';
        }
        // 扣除保证金
        if ($inArr['balance_deduction'] > 0) {
            //$changedata['by_id'] = $order_id;
            //$changedata['change_desc'] = '订单抵扣';
            //$changedata['change_type'] = 2;
            //$changedata['earnest_money'] = $inArr['balance_deduction'] * -1;
            //$res = (new WalletModel())->change($changedata, $this->userInfo['user_id'], false);
            //if ($res !== true) {
            //    Db::rollback();// 回滚事务
            //    return '预先扣除保证金失败';
            //}
            //$res = $this->where('order_id', $order_id)->update(['is_deduct' => 1]);
            //if ($res < 1) {
            //    Db::rollback();// 回滚事务
            //    return '更改保证金扣除状态失败';
            //}
            //扣除保证金->统计
//            ($user_id,$order_id,$proxy_pid,$type,$status,$price,$content="")
            $user_bond = (new ProxyUsersModel())->info($this->userInfo['user_id']);
            bondLog($this->userInfo['user_id'], $order_id, $user_bond['pid'], 1, 4, $inArr['balance_deduction'], '进货');
        }
//        var_dump($cartList);die;
        $res = $this->addOrderGoods($cartList, $order_id);//写入商品
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return $res;
        }
        if ($purchaseType == 3) {//提现下单时扣除库存，云仓与现货，跳转支付时处理
            $this->warehouseSplit($order_id);

            $res = $StockModel->stockBySupply($order_id, $cartList, $purchaseType, $this->userInfo['user_id'], 'DESC');
            if ($res == false) {
                Db::rollback();//回滚事务
                return '扣库存失败，不能完成此操作.';
            }
        }
        Db::commit(); // 提交事务

        return true;
    }
    /*------------------------------------------------------ */
    //-- 订单拆分
    /*------------------------------------------------------ */
    public function warehouseSplit($order_id)
    {
        Db::startTrans();
        try {
            //获取订单
            $shop_order_info = Db::name('channel_order_info')->where(array('order_id' => $order_id))->find();

            $order_sn = $shop_order_info['order_sn'];
            $old_order_id = $shop_order_info['order_id'];


            //获取订单商品表
            $shop_order_goods = Db::name('channel_order_goods')->where(array('order_id' => $order_id))->group('warehouse_id')->select();


            //判断是否需要拆单
            if (count($shop_order_goods) > 1) {
                //执行拆单
                foreach ($shop_order_goods as $k => $v) {
                    if ($v['discount'] == 0) {
                        unset($shop_order_info['discount']);
                    }
                    unset($shop_order_info['order_id']);
                    $shop_order_info['is_split'] = 1;
                    $shop_order_info['pid'] = $old_order_id;
                    $shop_order_info['order_sn'] = $order_sn . '_' . $k;
                    $shop_order_info['warehouse_id'] = $v['warehouse_id'];
                    //商品总额
                    $shop_order_info['goods_amount'] = $v['goods_number'] * $v['sale_price'];
                    $new_id = Db::name('channel_order_info')->insertGetId($shop_order_info);
                    if ($new_id) {
                        //更新关联订单
                        Db::name('channel_order_goods')->where(array('rec_id' => $v['rec_id']))->update(array('order_id' => $new_id));
                    }
                }
            } else {
                Db::name('channel_order_info')->where(array('order_id' => $order_id))->update(array('warehouse_id' => $shop_order_goods[0]['warehouse_id']));
            }
            Db::commit();
        } catch (\Exception $msg) {
            Db::rollback();
        }
    }


    /*------------------------------------------------------ */
    //-- 写入订单商品
    //-- $cartList array 购物车列表
    //-- $order_id int 订单ID
    /*------------------------------------------------------ */
    public function addOrderGoods(&$cartList, $order_id)
    {
        $add_time = time();
        $orderGoods = [];
        $rec_ids = [];
        foreach ($cartList as $key => $cg) {
            $warehouse_id = Db::name('shop_goods')->where(array('goods_id' => $cg['goods_id']))->field('warehouse_id')->find()['warehouse_id'];

            $goods = array(
                'order_id' => $order_id,
                'brand_id' => $cg['brand_id'],
                'warehouse_id' => $warehouse_id,
                'cid' => $cg['cid'],
                'goods_id' => $cg['goods_id'],
                'goods_name' => $cg['goods_name'],
                'sku_id' => $cg['sku_id'],
                'sku_val' => $cg['sku_val'],
                'sku_name' => $cg['sku_name'],
                'goods_sn' => $cg['goods_sn'],
                'sale_price' => $cg['sale_price'],
                'price_type' => $cg['price_type'],
                'goods_weight' => $cg['goods_weight'],
                'base_unit' => $cg['base_unit'],
                'base_unit_name' => $cg['base_unit_name'],
                'goods_unit' => $cg['goods_unit'],
                'goods_unit_name' => $cg['goods_unit_name'],
                'convert_unit_val' => $cg['convert_unit_val'],
                'goods_number' => $cg['goods_number'],
                'real_number' => $cg['real_number'],
                'balance_deduction' => $cg['balance_deduction'],
                'pic' => $cg['pic'],
                'add_time' => $add_time,
            );
            $orderGoods[] = $goods;
            $rec_ids[] = $cg['rec_id'];
        }
        $res = (new OrderGoodsModel)->insertAll($orderGoods);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '未知原因，订单商品写入失败.';
        }

        $where[] = ['rec_id', 'in', $rec_ids];
        (new CartModel)->where($where)->delete();// 清理购物车的商品
        return true;
    }

    /*------------------------------------------------------ */
    //-- 订单信息
    //-- $order_id int 订单ID
    //-- $iscache bool 是否读取缓存
    //-- $loginUid int 当前登陆会员ID
    /*------------------------------------------------------ */
    public function info($order_id, $iscache = true, $loginUid = 0)
    {
        static $StockModel;
        if ($iscache == true) {
            $orderInfo = Cache::get($this->mkey . $order_id);
        }
        if (empty($orderInfo) == true) {

            $admins = \think\facade\Session::get('main_admin');
            if ($admins['info']['warehouse_id'] != '' && $admins['info']['warehouse_id'] != 0) {
                $wheres[] = ['warehouse_id', 'in', $admins['info']['warehouse_id']];
            }
            $wheres[] = ['order_id', '=', $order_id];
//            var_dump($wheres);die;
            $orderInfo = $this->where($wheres)->find();

            if (empty($orderInfo)) return [];
            $orderInfo = $orderInfo->toArray();
            $orderGoods = $this->orderGoods($order_id);
            $orderInfo['goodsList'] = $orderGoods['goodsList'];
            $orderInfo['goodsNum'] = $orderGoods['goodsNum'];
            $orderInfo['_add_time'] = dateTpl($orderInfo['add_time'], 'Y-m-d H:i:s', true);
            $orderInfo['_pay_time'] = dateTpl($orderInfo['pay_time'], 'Y-m-d H:i:s', true);
            $orderInfo['_shipping_time'] = dateTpl($orderInfo['shipping_time'], 'Y-m-d H:i:s', true);
            $orderInfo['_cancel_time'] = dateTpl($orderInfo['cancel_time'], 'Y-m-d H:i:s', true);
            $orderInfo['offlineimg'] = explode(',', $orderInfo['offlineimg']);
            $orderInfo['margin_audit_img'] = explode(',', $orderInfo['margin_audit_img']);
            $ProxyUsersModel = new ProxyUsersModel();
            $userInfo = $ProxyUsersModel->info($orderInfo['user_id']);
            $orderInfo['user_name'] = $userInfo['byUser']['real_name'];
            if ($orderInfo['supply_uid'] > 0) {
                $supplyUserInfo = $ProxyUsersModel->info($orderInfo['supply_uid']);
                $orderInfo['supply_name'] = $supplyUserInfo['byUser']['real_name'];
                $orderInfo['supply_mobile'] = $supplyUserInfo['byUser']['mobile'];
            } else {
                $orderInfo['supply_name'] = '厂家';
            }
            if (empty($StockModel)) {
                $StockModel = new StockModel();
            }
            $orderInfo['lackGoods'] = 0;
            if ($orderInfo['is_stock'] == 0 && $orderInfo['purchase_type'] <= 3) {//云仓、现货订单未扣库存，判断库存是否满足
                $lackGoods = $StockModel->checkLackGoods($orderInfo['goodsList'], $orderInfo['supply_uid'], $orderInfo['purchase_type']);
                if (empty($lackGoods) == false) {
                    $orderInfo['lackGoods'] = 1;
                }
            }
            if ($orderInfo['is_pay_eval'] == 1) {//支付成功后待执行处理
                asynRun('channel/OrderModel/asynRunPaySuccessEval', ['order_id' => $orderInfo['order_id']]);//异步执行
            }
            Cache::set($this->mkey . $order_id, $orderInfo, 20);
        }
        $orderInfo['isConfirmed'] = 0;//确认订单
        $orderInfo['isCancel'] = 0;//取消订单
        $orderInfo['isDelete'] = 0;//删除订单
        $orderInfo['isPay'] = 0;//支付订单
        $orderInfo['isSign'] = 0;//签收订单
        $orderInfo['isCheckPay'] = 0;//审核付款
        $orderInfo['isAddStock'] = 0;//一键补货
        $orderInfo['isShipping'] = 0;//发货
        $config = config('config.');

        $orderInfo['purchase_type_name'] = $config['purchaseTypeList'][$orderInfo['purchase_type']];
        if ($orderInfo['pay_status'] == $config['PS_WAITCHACK']) {
            $orderInfo['ostatus'] = '付款待审核';
            if ($loginUid == $orderInfo['supply_uid'] && $orderInfo['supply_uid'] == $orderInfo['payee_uid']) {
                $orderInfo['isCheckPay'] = 1;
            }

        } elseif ($orderInfo['order_status'] == $config['OS_UNCONFIRMED']) {//订单未确定
            if ($orderInfo['pay_status'] == $config['PS_CHACKERROR']) {
                $orderInfo['ostatus'] = '付款审核失败';
                $orderInfo['isCancel'] = 1;//可操作：取消
                $orderInfo['isPay'] = 1;//可操作：支付
            } elseif ($orderInfo['lackGoods'] == 1) {
                $orderInfo['ostatus'] = '缺货';
                if ($orderInfo['supply_uid'] == $loginUid) {
                    $orderInfo['isAddStock'] = 1;
                } else {
                    $orderInfo['isPay'] = 1;
                }
            } elseif ($orderInfo['is_pay'] == 1 && $orderInfo['pay_status'] == $config['PS_UNPAYED']) {
                $orderInfo['isCancel'] = 1;//可操作：取消
                $orderInfo['isPay'] = 1;//可操作：支付
                $orderInfo['ostatus'] = '待付款';
            } else {
                $orderInfo['ostatus'] = '待确认';
                $orderInfo['isCancel'] = 1;//可操作：取消
                if ($orderInfo['supply_uid'] == $loginUid) {
                    $orderInfo['isConfirmed'] = 1;//可操作：确认订单
                }
            }
        } elseif ($orderInfo['order_status'] == $config['OS_CONFIRMED']) {
            if ($orderInfo['shipping_status'] == $config['SS_UNSHIPPED']) {
                $orderInfo['ostatus'] = '待发货';
                if ($orderInfo['purchase_type'] == 2 && $orderInfo['supply_uid'] == $loginUid) {//现货订单前端才有发货权限，提货订单为云仓提货，由平台发货
                    $orderInfo['isShipping'] = 1;
                }
            } elseif ($orderInfo['shipping_status'] == $config['SS_SHIPPED']) {
                $orderInfo['ostatus'] = '已发货';
                if ($orderInfo['user_id'] == $loginUid) {
                    $orderInfo['isSign'] = 1;
                }
            } elseif ($orderInfo['shipping_status'] == $config['SS_SIGN']) {
                if ($orderInfo['purchase_type'] == 1) {
                    $orderInfo['ostatus'] = '已入仓';
                } else {
                    $orderInfo['ostatus'] = '已完成';
                }
            }
            if ($orderInfo['is_pay'] > 0 && $orderInfo['pay_status'] == $config['PS_UNPAYED']) {
                $orderInfo['isCancel'] = 1;//可操作：取消
                $orderInfo['isPay'] = 1;//可操作：支付
                $orderInfo['ostatus'] = '待付款';
            }
        } else {
            if ($orderInfo['is_delete'] == 1) {
                $orderInfo['ostatus'] = '已删除';
            } else {
                $orderInfo['isDelete'] = 1;
                $orderInfo['ostatus'] = '已取消';
            }
        }
        return $orderInfo;
    }
    /*------------------------------------------------------ */
    //-- 获取订单商品
    //-- $order_id int 订单ID
    /*------------------------------------------------------ */
    public function orderGoods($order_id)
    {
        static $OrderGoodsModel;
        if (empty($OrderGoodsModel)) {
            $OrderGoodsModel = new OrderGoodsModel();
        }
        $rows = $OrderGoodsModel->order('rec_id ASC')->where('order_id', $order_id)->select()->toArray();
        if (empty($rows)) return [];
        $goodsNum = 0;
        foreach ($rows as $key => $row) {
            $row['exp_price'] = explode('.', $row['sale_price']);
            $goodsList[] = $row;
            $goodsNum += $row['goods_number'];
        }
        return ['goodsList' => $goodsList, 'goodsNum' => $goodsNum];
    }
    /*------------------------------------------------------ */
    //-- 获取订单商品
    //-- $upDate array 更新订单
    /*------------------------------------------------------ */
    public function upInfo($upData)
    {
        $order_id = $upData['order_id'];
        unset($upData['order_id']);
        $upData['update_time'] = time();
        $res = $this->where('order_id', $order_id)->update($upData);
        if ($res < 1) {
            return false;
        }
        $this->cleanMemcache($order_id);
        return true;
    }
    /*------------------------------------------------------ */
    //-- 获取订单数量统计 不包括已完成，已取消
    /*------------------------------------------------------ */
    public function getNum()
    {
        $mkey = $this->mkey . 'getNum' . $this->userInfo['user_id'];
        $data = Cache::get($mkey);
        if (empty($data) == false) return $data;
        $where[] = ['user_id', '=', $this->userInfo['user_id']];
        $where[] = ['order_status', 'in', [0, 1]];
        $where[] = ['shipping_status', '<', 2];
        $rows = $this->where($where)->field('count(order_id) as num,purchase_type')->group('purchase_type')->select();
        $data = [];
        foreach ($rows as $row) {
            $data[$row['purchase_type']] = $row['num'];
        }
        Cache::set($mkey, $data, 15);
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 获取下级订单数量统计 不包括已完成，已取消
    /*------------------------------------------------------ */
    public function getSubNum()
    {
        $mkey = $this->mkey . 'getSubNum' . $this->userInfo['user_id'];
        $data = Cache::get($mkey);
        if (empty($data) == false) return $data;
        $where[] = ['supply_uid', '=', $this->userInfo['user_id']];
        $where[] = ['order_status', 'IN', [0, 1]];
        $where[] = ['shipping_status', '=', 0];
        $where[] = ['pay_status', '<>', 11];
        $rows = $this->where($where)->field('count(order_id) as num,purchase_type')->group('purchase_type')->select();
        $data = [];
        foreach ($rows as $row) {
            $data[$row['purchase_type']] = $row['num'];
        }
        Cache::set($mkey, $data, 15);
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 确定订单操作
    //-- $order_id int 订单ID
    //-- $supply_uid int 供货代理ID，前端调用必须传，用于识别是否正确供货代理操作
    /*------------------------------------------------------ */
    public function confirmed($order_id, $supply_uid = 0)
    {
        $orderInfo = $this->info($order_id);
        $operating = $this->operating($orderInfo);//订单操作权限
        if ($operating['confirmed'] !== true) {
            return '订单当前状态不能进行此操作.';
        }
        if ($supply_uid > 0 && $supply_uid != $orderInfo['supply_uid']) {
            return '你无权操作.';
        }
        Db::startTrans();//开启事务
        if ($orderInfo['is_stock'] == 0) {//未扣库存，尝试执行扣库存
            $StockModel = new StockModel();
            //执行扣库存
            $res = $StockModel->evalSupplyStock($orderInfo);
            if ($res == false) {
                Db::rollback();//回滚事务
                return '扣库存失败，不能完成此操作.';
            }
            $upData['is_stock'] = 1;
        }
        $upData['order_id'] = $order_id;
        $upData['order_status'] = 1;
        $upData['confirm_time'] = time();
        $upData['cancel_time'] = 0;
        $upData['money_paid'] = 0;
        $res = $this->upInfo($upData);
        if ($res !== true) {
            Db::rollback();//回滚事务
            return '更新订单失败.';
        }
        Db::commit();//提交事务
        $orderInfo['order_status'] = $upData['order_status'];
        if ($supply_uid > 0) {
            $this->_log($orderInfo, '设为已确认');
        } else {
            $this->_log($orderInfo, '后台设为已确认');
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 线下支付收款确认
    //-- $orderInfo array 订单信息
    //-- $operate int 审核结果
    //-- $supply_uid int 供货代理ID，前端调用必须传，用于识别是否正确供货代理操作
    /*------------------------------------------------------ */
    public function cfmCodPay(&$orderInfo, $operate, $transaction_id = '', $supply_uid)
    {
        if ($orderInfo['pay_code'] != 'offline') {
            return '非线下打款订单，不允许操作.';
        }
        if ($supply_uid > 0 && $supply_uid != $orderInfo['supply_uid']) {
            return '你无权操作此订单.';
        }
        $operating = $this->operating($orderInfo);//订单操作权限
        if ($operating['cfmCodPay'] !== true) {
            return '订单当前状态不能操作线下打款确认.';
        }
        $config = config('config.');
        //审核失败
        if ($operate == 2) {
            if ($orderInfo['is_deduct'] == 1 && $orderInfo['balance_deduction'] > 0) { // 返回保证金
                $changedata['by_id'] = $orderInfo['order_id'];
                $changedata['change_desc'] = '订单抵扣退还';
                $changedata['change_type'] = 2;
                $changedata['earnest_money'] = $orderInfo['balance_deduction'];
                $res = (new WalletModel())->change($changedata, $orderInfo['user_id'], false);
                if ($res !== true) {
                    Db::rollback();//回滚事务
                    return '保证金抵扣退还失败';
                }
                $upData['is_deduct'] = 0;
            }

            $upData['order_id'] = $orderInfo['order_id'];
            $upData['pay_status'] = $config['PS_CHACKERROR'];
            $upData['transaction_id'] = $transaction_id;
            $upData['cfmpay_user'] = $supply_uid > 0 ? $supply_uid : AUID;
            $res = $this->upInfo($upData);
            if ($res !== true) {
                return '操作失败.';
            }
            $orderInfo['pay_status'] = $upData['pay_status'];
            $res = $this->_log($orderInfo, '线下打款，审核失败：' . $transaction_id);
            if ($res < 1) {
                return '订单日志写入失败，请重试.';
            }
            return true;
        }
        //审核失败end

        $time = time();
        $upData['order_status'] = $config['OS_CONFIRMED'];
        $upData['pay_status'] = $config['PS_PAYED'];
        if ($orderInfo['confirm_time'] < 1) {
            $upData['confirm_time'] = $time;
        }
        $upData['pay_time'] = $time;
        $upData['money_paid'] = $orderInfo['order_amount'];
        $upData['cfmpay_user'] = $supply_uid > 0 ? $supply_uid : AUID;
        $upData['order_id'] = $orderInfo['order_id'];
        $upData['is_pay_eval'] = 1;//设为待执行支付成功后的相关处理

        Db::startTrans();//开启事务
        $upData['is_deduct'] = 1;
        if ($orderInfo['is_deduct'] == 0 && $orderInfo['balance_deduction'] > 0) { // 保证金未抵扣
            $changedata['by_id'] = $orderInfo['order_id'];
            $changedata['change_desc'] = '订单抵扣';
            $changedata['change_type'] = 2;
            $changedata['earnest_money'] = $orderInfo['balance_deduction'] * -1;
            $res = (new WalletModel())->change($changedata, $orderInfo['user_id'], false);
            if ($res !== true) {
                Db::rollback();//回滚事务
                return '保证金抵扣失败';
            }
            $upData['is_deduct'] = 1;
            //扣除保证金->统计
            $user_bond = (new ProxyUsersModel())->info($orderInfo['user_id']);
            bondLog($orderInfo['user_id'], $orderInfo['order_id'], $user_bond['pid'], 1, 3, $orderInfo['balance_deduction'], '进货');
        }
        $res = $this->upInfo($upData);
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return '操作失败.';
        }
        $orderInfo['order_status'] = $upData['order_status'];
        $orderInfo['pay_status'] = $upData['pay_status'];
        if ($supply_uid > 0) {
            $log = '线下打款，供货代理收款确认：' . $transaction_id;
        } else {
            $log = '线下打款，后台收款确认：' . $transaction_id;
        }
        $res = $this->_log($orderInfo, $log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }

        Db::commit();//提交事务
        //file_put_contents('order_pay.log', json_encode($orderInfo), FILE_APPEND);
        asynRun('channel/OrderModel/asynRunPaySuccessEval', ['order_id' => $orderInfo['order_id']]);//异步执行
        return true;
    }
    /*------------------------------------------------------ */
    //-- 设置为未付款
    /*------------------------------------------------------ */
    public function setUnPay($order_id)
    {
        $config = config('config.');
        $upData['order_status'] = 0;
        $orderInfo['order_status'] = $upData['order_status'];
        $upData['order_id'] = $order_id;
        $upData['pay_status'] = $config['PS_WAITCHACK'];
        $upData['shipping_status'] = $config['SS_UNSHIPPED'];
        $upData['pay_time'] = 0;
        $upData['money_paid'] = 0;
        Db::startTrans();//开启事务
        $res = $this->upInfo($upData);
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return '操作失败.';
        }
        $orderInfo = $this->info($order_id);
        $log = '撤回付款审核';
        $res = $this->_log($orderInfo, $log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }
        if ($orderInfo['purchase_type'] == 1) {//云仓订单
            $res = (new StockModel)->stockByBuy($orderInfo, 'DEC', $log);//库存处理减库存
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
            $res = (new RewardLogModel)->returnReward('order', $order_id, 'return', '撤回付款审核,撤销：');//退回相关奖励
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
        }
        Db::commit();//提交事务

        return true;
    }
    /*------------------------------------------------------ */
    //-- 订单支付成功处理，在线支付回调执行
    /*------------------------------------------------------ */
    public function updatePay($upData = [], $_log = '支付成功')
    {
        unset($upData['order_amount']);
        $config = config('config.');
        $upData['pay_status'] = $config['PS_PAYED'];
        $upData['order_status'] = $config['OS_CONFIRMED'];
        $upData['pay_time'] = time();
        $upData['is_pay_eval'] = 1;//设为待执行支付成功后的相关处理
        $res = $this->upInfo($upData);
        if ($res !== true) {
            return false;
        }
        $orderInfo = $this->find($upData['order_id'])->toArray();
        $this->_log($orderInfo, $_log);

        asynRun('channel/OrderModel/asynRunPaySuccessEval', ['order_id' => $orderInfo['order_id']]);//异步执行
        return true;
    }
    /*------------------------------------------------------ */
    //-- 订单支付成功后异步处理
    /*------------------------------------------------------ */
    public function asynRunPaySuccessEval($param)
    {
        //file_put_contents('order_pay.log', '***************************asynRunPaySuccessEval***********************', FILE_APPEND);
        //file_put_contents('order_pay.log', json_encode($param), FILE_APPEND);
        $order_id = $param['order_id'] * 1;//异步执行传入必须强制类型
        if ($order_id < 1) {
            return '缺少订单ID';
        }
        $mkey = 'payChannelSuccessEvalIng' . $order_id;
        $res = redisLook($mkey, 10);//redis锁
        if ($res == false) return true;

        $orderInfo = $this->info($order_id, false);
        if ($orderInfo['is_pay_eval'] != 1) {
            return true;
        }
        $config = config('config.');
        $upData = [];
        if ($orderInfo['purchase_type'] == 1 && $orderInfo['shipping_status'] != $config['SS_SIGN']) {//云仓订单库存处理
            Db::startTrans();//开启事务
            //库存处理
            $res = (new StockModel)->stockByBuy($orderInfo, 'INC', '支付成功');
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
            //库存处理end
            $upData['shipping_status'] = $config['SS_SIGN'];//云仓订单直接为已完成
            $upData['sign_time'] = time();//云仓订单直接为已完成
            $res = $this->where('order_id', $orderInfo['order_id'])->update($upData);
            if ($res < 1) {
                Db::rollback();// 回滚事务
                $this->_log($orderInfo, '增加云库存失败');
                return '增加云库存更新订单状态失败';
            }
            Db::commit();// 提交事务
        }
        if ($orderInfo['is_reward'] == 0) {//奖励处理
            Db::startTrans();//开启事务
            $RewardLogModel = new RewardLogModel();
            $res = $RewardLogModel->runByOrder($orderInfo);//计算奖项
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
            if ($orderInfo['purchase_type'] == 1) {//云仓订单，立返奖项
                $res = $RewardLogModel->runRewardToUserByOrder($orderInfo['order_id']);
                if ($res !== true) {
                    Db::rollback();// 回滚事务
                    return $res;
                }
            } else {
                $res = $RewardLogModel->runRewardUpByOrder($orderInfo['order_id']);
                if ($res !== true) {
                    Db::rollback();// 回滚事务
                    return $res;
                }
            }

            Db::commit();// 提交事务
            $upData['is_reward'] = 1;
        }
        $upData['is_pay_eval'] = 2;
        $this->where('order_id', $orderInfo['order_id'])->update($upData);
        $this->cleanMemcache($orderInfo['order_id']);
        redisLook($mkey, -1);//销毁锁
        return true;
    }
    /*------------------------------------------------------ */
    //-- 取消订单操作
    //-- $order_id int 订单ID
    //-- $user_id int 会员ID 前端取消时必须传
    /*------------------------------------------------------ */
    public function cancel(&$orderInfo, $user_id = 0)
    {
        if (empty($orderInfo)) {
            return '没有找到相关订单信息.';
        }

        if ($user_id > 0 && $orderInfo['user_id'] != $user_id) {
            if ($orderInfo['isCancel'] == 0) {
                return '当前订单不能操作取消.';
            }
            if ($orderInfo['supply_uid'] == $user_id) {//供货代理操作取消
                if ($orderInfo['add_time'] > strtotime("+1 our")) {
                    return '下级订单超过1小时才能操作取消.';
                }
            } else {
                return '你无权进行操作.';
            }
        }
        Db::startTrans();//启动事务
        if ($orderInfo['is_deduct'] == 1 && $orderInfo['balance_deduction'] > 0) { // 返回保证金
            $changedata['by_id'] = $orderInfo['order_id'];
            $changedata['change_desc'] = '订单抵扣退还';
            $changedata['change_type'] = 2;
            $changedata['earnest_money'] = $orderInfo['balance_deduction'];
            $res = (new WalletModel())->change($changedata, $orderInfo['user_id'], false);
            if ($res !== true) {
                Db::rollback();//回滚事务
                return '保证金抵扣退还失败';
            }
            $upData['is_deduct'] = 0;
        }elseif ($orderInfo['is_deduct'] == 0){
            //取消订单,删除待还记录
            $user_bond = (new ProxyUsersModel())->info($orderInfo['user_id']);
            bondLog($orderInfo['user_id'], $orderInfo['order_id'], $user_bond['pid'], 1, 5, $orderInfo['balance_deduction'], '进货');
        }
        $upData['is_stock'] = 0;
        $upData['order_id'] = $orderInfo['order_id'];
        $upData['order_status'] = config('config.OS_CANCELED');
        $upData['cancel_time'] = time();
        $res = $this->upInfo($upData);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '操作失败，请重试.';
        }
        if ($user_id > 0) {
            if ($orderInfo['user_id'] == $user_id) {
                $log = '用户取消订单';
            } elseif ($orderInfo['supply_uid'] == $user_id) {//供货代理操作取消
                $log = '拿货上级取消订单';
            }
        } else {
            $log = '后台取消订单';
        }
        $orderInfo['order_status'] = $upData['order_status'];
        $res = $this->_log($orderInfo, $log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }
        if ($orderInfo['is_stock'] == 1) {//已扣库存的，返还库存到供货人
            $res = (new StockModel)->evalSupplyStock($orderInfo, 'INC');
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
        }
        //退回相关奖励
        $RewardLogModel = new RewardLogModel();
        $res = $RewardLogModel->returnReward('order', $orderInfo['order_id'], 'cancel');
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return $res;
        }
        $user_bond = (new ProxyUsersModel())->info($orderInfo['user_id']);
        bondLog($orderInfo['user_id'], $orderInfo['order_id'], $user_bond['pid'], 1, 5, $orderInfo['balance_deduction'], '取消订单');

        Db::commit();//提交事务
        return true;
    }
    /*------------------------------------------------------ */
    //-- 订单退款操作
    //-- $order_id int 订单ID
    /*------------------------------------------------------ */
    public function returnPay($order_id)
    {
        $orderInfo = $this->info($order_id);
        $operating = $this->operating($orderInfo);//订单操作权限
        if ($operating['returnPay'] !== true) {
            return '订单当前状态不能操作退款.';
        }
        $config = config('config.');
        Db::startTrans();//开启事务
        if ($orderInfo['is_deduct'] == 1 && $orderInfo['balance_deduction'] > 0) { // 返回保证金
            $changedata['by_id'] = $orderInfo['order_id'];
            $changedata['change_desc'] = '订单抵扣退还';
            $changedata['change_type'] = 2;
            $changedata['earnest_money'] = $orderInfo['balance_deduction'];
            $res = (new WalletModel())->change($changedata, $orderInfo['user_id'], false);
            if ($res !== true) {
                Db::rollback();//回滚事务
                return '保证金抵扣退还失败';
            }
            $upData['is_deduct'] = 0;
        }
        $upData['order_id'] = $order_id;
        $upData['pay_status'] = $config['PS_RUNPAYED'];
        $res = $this->upInfo($upData);
        if ($res !== true) {
            Db::rollback();//回滚
            return '更新订单失败.';
        }
        $orderInfo['pay_status'] = $upData['pay_status'];
        $res = $this->_log($orderInfo, '设为退款');
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }
        $WalletModel = new WalletModel();
        if ($orderInfo['money_paid'] > 0) {
            if (in_array($orderInfo['pay_code'], ['balance', 'offline'])) {//线下打款和余额支付，退回余额
                $changedata['by_id'] = $orderInfo['order_id'];
                $changedata['change_desc'] = '订单退款到余额:' . $orderInfo['money_paid'];
                $changedata['change_type'] = 2;
                $changedata['balance_money'] = $orderInfo['money_paid'];
                $res = $WalletModel->change($changedata, $orderInfo['user_id'], false);
                if ($res != true) {
                    Db::rollback();//回滚
                    return '订单退款到余额失败.';
                }
            } elseif ($orderInfo['pay_code'] == 'goodsMoney') {//货款退回货款
                $changedata['by_id'] = $orderInfo['order_id'];
                $changedata['change_desc'] = '订单退款到货款余额:' . $orderInfo['money_paid'];
                $changedata['change_type'] = 3;
                $changedata['goods_money'] = $orderInfo['money_paid'];
                $changedata['goods_money_outlay_total'] = $orderInfo['money_paid'] * -1;
                $res = $WalletModel->change($changedata, $orderInfo['user_id'], false);
                if ($res != true) {
                    Db::rollback();//回滚
                    return '订单退款到余额失败.';
                }
            } elseif ($orderInfo['pay_code'] == 'upLevelgoodsMoney') {//货款退回货款
                $changedata['by_id'] = $orderInfo['order_id'];
                $changedata['change_desc'] = '订单退款到升级货款余额:' . $orderInfo['money_paid'];
                $changedata['change_type'] = 5;
                $changedata['uplevel_goods_money'] = $orderInfo['money_paid'];
                $res = $WalletModel->change($changedata, $orderInfo['user_id'], false);
                if ($res != true) {
                    Db::rollback();//回滚
                    return '订单退款到余额失败.';
                }
            } else {//在线退款，必须放最后
                $code = str_replace('/', '\\', "/payment/" . $orderInfo['pay_code'] . "/" . $orderInfo['pay_code']);
                $payment = new $code();
                $orderInfo['refund_amount'] = $orderInfo['money_paid'];//实付金额
                $res = $payment->refund($orderInfo);
                if ($res !== true) {
                    Db::rollback();//回滚
                    return '请求退款接口失败：' . $res;
                }
            }
        }
        Db::commit();//提交事务
        return true;
    }

    /*------------------------------------------------------ */
    //-- 订单签收处理
    /*------------------------------------------------------ */
    public function sign(&$orderInfo)
    {
        $config = config('config.');
        $data['order_id'] = $orderInfo['order_id'];
        $data['shipping_status'] = $config['SS_SIGN'];
        $data['sign_time'] = time();
        Db::startTrans();//开启事务
        $res = $this->upInfo($data);
        if ($res !== true) {
            Db::rollback();//回滚事务
            return '更新失败.';
        }
        $orderInfo['shipping_status'] = $data['shipping_status'];
        $res = $this->_log($orderInfo, '后台操作设为签收');
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }
        if ($orderInfo['purchase_type'] > 1) {//非云仓订单
            $res = (new StockModel)->stockByBuy($orderInfo, 'INC', '订单签收增加库存');
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
            $res = (new RewardLogModel)->runRewardToUserByOrder($orderInfo['order_id']);//执行相关奖励到帐
            if ($res !== true) {
                Db::rollback();// 回滚事务
                return $res;
            }
        }
        Db::commit();//提交事务
        return true;
    }
    /*------------------------------------------------------ */
    //-- 设为未签收处理
    /*------------------------------------------------------ */
    public function setUnSign(&$orderInfo)
    {
        $config = config('config.');
        $upData['order_id'] = $orderInfo['order_id'];
        $upData['shipping_status'] = $config['SS_SHIPPED'];
        Db::startTrans();//开启事务
        $res = $this->upInfo($upData);
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return '操作失败.';
        }
        $orderInfo['shipping_status'] = $config['SS_SHIPPED'];
        $log = '设为未签收';
        $res = $this->_log($orderInfo, $log);
        if ($res < 1) {
            Db::rollback();// 回滚事务
            return '订单日志写入失败，请重试.';
        }
        $res = (new StockModel)->stockByBuy($orderInfo, 'DEC', $log);//库存处理减库存
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return $res;
        }
        $res = (new RewardLogModel)->returnReward('order', $orderInfo['order_id'], 'return', '设为未签收,撤销：');//退回相关奖励
        if ($res !== true) {
            Db::rollback();// 回滚事务
            return $res;
        }
        Db::commit();//提交事务
        return true;
    }

    /**订单后台操作权限
     * @param array $order 订单详情
     * @param bool $by_supplyer 供应商管理
     * @return array
     */
    public function operating(&$order)
    {
        $os = $order['order_status'];
        $ss = $order['shipping_status'];
        $ps = $order['pay_status'];
        $time = time();
        $config = config('config.');
        if ($os == $config['OS_UNCONFIRMED']) {//未确认
            $operating['isCancel'] = true;//取消
            if ($order['purchase_type'] > 1) {
                $operating['editConsignee'] = true; //修改收货信息
            }

            if ($order['pay_status'] >= 10) {
                $operating['cfmCodPay'] = true;//设为已付款
            }
        } elseif ($os == $config['OS_CONFIRMED']) { //已确认
            if ($ss == $config['SS_UNSHIPPED']) {//未发货
                $operating['isCancel'] = true;
                if ($ps == $config['PS_UNPAYED']) {//未支付


                } elseif ($ps == $config['PS_PAYED']) {//已支付
                    if ($order['pay_status'] == 'offline') {
                        $operating['setUnPay'] = true;//撤回付款审核
                    }
                }
                if ($order['purchase_type'] > 1) {
                    $operating['shipping'] = true;
                    $operating['editConsignee'] = true; //修改收货信息
                }
            } elseif ($ss == $config['SS_SHIPPED']) {//已发货
                $operating['sign'] = true;
                $operating['unshipping'] = true;//设为未发货
                $operating['returned'] = true;//设为退货

            } elseif ($ss == $config['SS_SIGN']) {//已签收/已入仓
                if ($order['purchase_type'] == 1) {
                    if ($order['pay_status'] == 'offline' && $order['sign_time'] > $time - 86400) {
                        $operating['setUnPay'] = true;//撤回付款审核
                    }
                } else {
                    if (($order['sign_time'] > $time - 604800)) {
                        $operating['unsign'] = true;//设为未签收
                    }
                }
                unset($operating['unconfirmed']);
            } else {
                $operating['isCancel'] = true;
            }
        } elseif ($os == $config['OS_RETURNED']) { //已退货
            if ($ps == $config['PS_PAYED']) {//退货后可操作退款
                $operating['returnPay'] = true;
            }
        } elseif ($os == $config['OS_CANCELED']) { //已关闭
            //涉及库存问题，取消订单不允许恢复
            //涉及库存问题，取消订单不允许恢复
            //涉及库存问题，取消订单不允许恢复
            if ($ps == $config['PS_PAYED']) {//取消后可操作退款
                $operating['returnPay'] = true;
            }
        }
        return $operating;
    }

    public function runReplenishment($istest = false)
    {
        $wait_time = 24 * 3600;
        if ($istest == true) { // 测试时使用
            $wait_time = 0;
        }
        //获取订单
        $orderList = $this->where('purchase_type', 1)
            ->where('pay_status', 0)
            ->where('order_status', 0)
            ->where('is_stock', 0)
            ->where('supply_uid', '>', 0)
            ->whereTime('add_time', '<=', time() - $wait_time)
            ->column('order_id');

        //获取上级未补货而且超过补货时间的订单
        foreach ($orderList as $order_id) {
            $orderInfo = $this->info($order_id);
            if ($orderInfo['is_stock'] != 0) continue;
            if ($orderInfo['is_deduct'] == 1 && $orderInfo['balance_deduction'] > 0) { // 返回保证金
                $changedata['by_id'] = $orderInfo['order_id'];
                $changedata['change_desc'] = '订单抵扣退还';
                $changedata['change_type'] = 2;
                $changedata['earnest_money'] = $orderInfo['balance_deduction'];
                $res = (new WalletModel())->change($changedata, $orderInfo['user_id'], false);
                if ($res !== true) { // 返回失败
                    continue;
                }
                $upData['is_deduct'] = 0;
            }
            $upData['order_id'] = $orderInfo['order_id'];
            $upData['order_amount'] = $orderInfo['order_amount'] + $orderInfo['balance_deduction'];
            $upData['balance_deduction'] = $orderInfo['balance_deduction'];
            $upData['supply_uid'] = 0;
            $upData['supply_proxy_id'] = 0;
            $upData['is_stock'] = 1;
            //订单归为平台
            $this->upInfo($upData);
            // $this->upInfo([
            //     'order_id' => $orderInfo['order_id'],
            //     'order_amount' => $orderInfo['order_amount'] + $orderInfo['balance_deduction'],
            //     'balance_deduction' => $orderInfo['balance_deduction'],
            //     'supply_uid' => 0,
            //     'supply_proxy_id' => 0,
            //     'is_stock' => 1,
            // ]);
        }
        return true;
    }

    public function getAllAmount($user_id)
    {
        $key = "get_all_amount_" . $user_id;
        $redis = Cache::get($key);
        if (empty($redis) == false) return $redis;
        $stats = $this->field('sum(balance_deduction + order_amount) as amount')->where('user_id', $user_id)
            ->where('order_status', 1)->where('pay_status', 1)->find();
        $amount = $stats['amount'] ?: 0;
        Cache::set($key, $amount, 60);
        return $amount;
    }

    /**
     * 提交返回保证金打款
     * @param $order_id
     * @param $user_id
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function returnBond($order_id, $user_id)
    {
        $post = request()->param();

        $update_data['margin_update_time'] = time();
        //保证金审核状态:0-待打款;1-(下级)待确认;2-已返;3-(下级)确认失败',
        switch ($post['status']) {
            case 1:
                if (!isset($post['margin_audit_img']) && count($post['margin_audit_img']) == 0) {
                    return '请上传凭证!';
                }
                $update_data['margin_audit_img'] = implode(',', $post['margin_audit_img']);
                $msg = "打款成功,待确认";
                $where['supply_uid'] = $user_id;
                $where['margin_audit_status'] = 0;

                $margin_audit_status = 1;
                break;
            case 2:
                $where['user_id'] = $user_id;
                $where['margin_audit_status'] = 1;
                $msg = "确认收款";
                $margin_audit_status = 2;
                break;
            case 3:
                $where['user_id'] = $user_id;
                $msg = "确认失败,代理商重新操作";
                $margin_audit_status = 3;
                $where['margin_audit_status'] = 1;
                break;
            case 4:
                $where['supply_uid'] = $user_id;
                $msg = "代理商审核不通过";
                $margin_audit_status = 4;
                $update_data['margin_audit_error'] = $post['remark'];
                break;
            default:
                return '类型有误!';
        }


        $update_data['margin_audit_status'] = $margin_audit_status;
        $where['order_id'] = $order_id;
        $where['surrender_order_status'] = 1;
        $channel_order_info = Db::name('channel_order_info')->where($where)->find();
        if (!$channel_order_info) {
            return '异常操作!';
        }
        if ($channel_order_info) {
            Db::startTrans();
            try {
                $order_info = Db::name('channel_order_info')->where($where)->update($update_data);
                if ($order_info) {
                    if ($post['status'] == 1) {
                        returnBond($user_id, $order_id, $msg, $post['margin_audit_img']);
                    }
                    if ($margin_audit_status == 2) {//用户操作点击确认收到保证金
                        $user_bond = (new ProxyUsersModel())->info($this->userInfo['user_id']);
                        bondLog($this->userInfo['user_id'], $order_id, $user_bond['pid'], 1, 3, $channel_order_info['balance_deduction'], $msg);

                    }
                    Db::commit();
                    return true;
                }
            } catch (Exception $e) {
                Db::rollback();
            }
        }

    }


    /**
     * 提交返回奖励保证金打款
     * @param $order_id
     * @param $user_id
     * @return string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function returnBondRecharge($order_id, $user_id)
    {
        $post = request()->param();

        $update_data['margin_update_time'] = time();
        //保证金审核状态:0-待打款;1-(下级)待确认;2-已返;3-(下级)确认失败',
        switch ($post['status']) {
            case 1:
                if (!isset($post['margin_audit_img']) && count($post['margin_audit_img']) == 0) {
                    return '请上传凭证!';
                }
                $update_data['margin_audit_img'] = implode(',', $post['margin_audit_img']);
                $msg = "打款成功,待确认";
//                $where['supply_uid'] = $user_id;

                $margin_audit_status = 1;
                break;
            case 2:
                $where['to_uid'] = $user_id;
                $msg = "确认收款";
                $margin_audit_status = 2;
                break;
            case 3:
                $where['to_uid'] = $user_id;
                $msg = "确认失败,代理商重新操作";
                $margin_audit_status = 3;
                break;
            case 4:
                $where['supply_uid'] = $user_id;
                $msg = "代理商审核不通过";
                $margin_audit_status = 4;
                $update_data['margin_audit_error'] = $post['remark'];
                break;
            default:
                return '类型有误!';
        }


        $update_data['margin_audit_status'] = $margin_audit_status;
        $where['id'] = $order_id;
//        $where['surrender_order_status'] = 1; //是否为 升级 奖励保证金0-否;1-是
        $channel_order_info = Db::name('channel_reward_log')->where($where)->find();
        if (!$channel_order_info) {
            return '异常操作!';
        }
        $order_info = Db::name('channel_reward_log')->where($where)->update($update_data);
        if ($order_info) {
            if ($post['status'] == 1) {
                returnBond($user_id, $order_id, $msg, $post['margin_audit_img'], 1);
            }
            return true;
        } else {
            return false;
        }
    }


    /**
     * 订单审核
     */
    public function deliveryAudit($order_id, $error, $status)
    {
        switch ($status) {
            case 2:
                $msg = "审核通过";
                break;
            case 3:
                $msg = "审核失败";
                break;
            default:
                return "参数有误";
        }
        $update['delivery_audit_status'] = $status;
        $update['delivery_audit_error'] = $error;
        $where['order_id'] = $order_id;
        $where['agent_pid'] = $this->userInfo['user_id'];
        $where['delivery_audit_status'] = 1;
        $where['order_status'] = 1;
        $order_info = Db::name('channel_order_info')->where($where)->find();
        if ($order_info) {
            $is_update = Db::name('channel_order_info')->where(array('order_id' => $order_info['order_id']))->update($update);
            if ($is_update) {
                if ($status == 3) {
                    #取消订单操作
                    $this->cancel($order_info, $order_info['user_id']);
                }
                //记录日志
                deliveryAuditLog($this->userInfo['user_id'], $order_id, $msg, $error);
                return true;
            }
        } else {
            return "参数有误!";
        }

    }


    /**
     * 保证金奖励
     */
    public function rewardDetail($order_id, $user_id)
    {

        $where['id'] = $order_id;
        $channel_reward_log = Db::name('channel_reward_log')->where($where)->find();
        if ($channel_reward_log['to_uid'] == $user_id) {
            $status = 1;
        } else {
            $status = 2;

        }
        $channel_reward_log['is_user'] = $status;
        #获取上级奖励用户
        $where_u['user_id'] = $channel_reward_log['supply_uid'];
        $channel_reward_log['pid_user'] = Db::name('users')->where($where_u)->field('user_id,real_name,mobile')->find();
        #获取奖励人手机号
        $where_us['user_id'] = $channel_reward_log['to_uid'];
        $channel_reward_log['pid_jl_user'] = Db::name('users')->where($where_us)->field('user_id,real_name,mobile')->find();

        #获取升级人信息
        $wheres['u.user_id'] = $channel_reward_log['from_uid'];
        $wheres['c.order_id'] = $channel_reward_log['from_id'];
        $channel_reward_log['user_level'] = Db::name('users')->alias('u')
            ->join('channel_proxy_users_recharge_log c', 'c.user_id=u.user_id')
            ->where($wheres)->field('u.user_id,u.real_name,u.mobile,c.pay_time,c.order_amount,c.current_proxy_id,c.proxy_id')->find();
        $current_proxy_id = $channel_reward_log['user_level']['current_proxy_id'];
        $proxy_id = $channel_reward_log['user_level']['proxy_id'];

        $data_proxy_level = Db::name('channel_proxy_level')->where('proxy_id', 'in', $current_proxy_id . ',' . $proxy_id)->field('proxy_name')->order('proxy_id desc')->select();
        $channel_reward_log['proxy_first_name'] = $data_proxy_level[0]['proxy_name'] ? $data_proxy_level[0]['proxy_name'] : '';
        $channel_reward_log['proxy_last_name'] = isset($data_proxy_level[1]['proxy_name']) ? $data_proxy_level[1]['proxy_name'] : '';

        #获取升级金额
        $channel_reward_log['margin_audit_img'] = explode(',', $channel_reward_log['margin_audit_img']);
        switch ($channel_reward_log['margin_audit_status']) {
            case 0:
                $msg = "待打款";
                break;
            case 1:
                $msg = "待确认";
                break;
            case 2:
                $msg = "已返";
                break;
            case 3:
                $msg = "确认失败";
                break;
            case 4:
                $msg = "审核失败";
                break;
        }
        $channel_reward_log['status'] = $msg;

        return $channel_reward_log;


    }

}
